import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import controller.Controller;


public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField urlBox;
	private JComboBox errorCorrectionComboBox;
	private JComboBox imageExtensionComboBox;
	private JCheckBox shortenURLCheckBox;
	private JComboBox imgSize;
	
	
	private String[] errorLevels = {"Low", "Medium","Quarter","High"};
	private String[] extensions = {"jpg", "png"};
	private String[] size = {"Small", "Medium", "Large", "X-Large"};
	private JTextField filePath;
	private JTextField fileName;
	
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 292);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblQrCodeGenerator = new JLabel("QR Code Generator");
		lblQrCodeGenerator.setBounds(314, 231, 119, 16);
		contentPane.add(lblQrCodeGenerator);
		
		JLabel lblUrl = new JLabel("URL");
		lblUrl.setBounds(6, 17, 37, 16);
		contentPane.add(lblUrl);
		
		urlBox = new JTextField("http://");
		urlBox.setBounds(45, 11, 270, 28);
		contentPane.add(urlBox);
		urlBox.setColumns(10);
		
		shortenURLCheckBox = new JCheckBox("Shorten URL");
		shortenURLCheckBox.setBounds(325, 13, 119, 23);
		contentPane.add(shortenURLCheckBox);
		
		JLabel lblErrorCorrectionLevel = new JLabel("Error Correction Level");
		lblErrorCorrectionLevel.setBounds(6, 118, 148, 16);
		contentPane.add(lblErrorCorrectionLevel);
		
		errorCorrectionComboBox = new JComboBox(errorLevels);
		errorCorrectionComboBox.setBounds(152, 114, 124, 27);
		contentPane.add(errorCorrectionComboBox);
		
		JLabel lblQrCodeImage = new JLabel("Extension");
		lblQrCodeImage.setBounds(6, 150, 70, 16);
		contentPane.add(lblQrCodeImage);
		
		imageExtensionComboBox = new JComboBox(extensions);
		imageExtensionComboBox.setBounds(152, 146, 124, 27);
		contentPane.add(imageExtensionComboBox);
		
		JButton generateButton = new JButton("Generate QR Code");
		generateButton.setBounds(6, 218, 270, 44);
		contentPane.add(generateButton);
		
		JLabel lblSaveTo = new JLabel("Save To");
		lblSaveTo.setBounds(6, 51, 61, 16);
		contentPane.add(lblSaveTo);
		
		filePath = new JTextField();
		filePath.setEditable(false);
		filePath.setBounds(68, 45, 247, 28);
		contentPane.add(filePath);
		filePath.setColumns(10);
		
		JButton btnChooseLocation = new JButton("Choose");
		btnChooseLocation.setBounds(319, 46, 114, 29);
		contentPane.add(btnChooseLocation);
		
		JLabel lblFileName = new JLabel("File Name");
		lblFileName.setBounds(6, 86, 70, 16);
		contentPane.add(lblFileName);
		
		fileName = new JTextField();
		fileName.setBounds(78, 80, 355, 28);
		contentPane.add(fileName);
		fileName.setColumns(10);
		
		JLabel lblSize = new JLabel("Size");
		lblSize.setBounds(6, 185, 61, 16);
		contentPane.add(lblSize);
		
		imgSize = new JComboBox(size);
		imgSize.setBounds(152, 181, 124, 27);
		contentPane.add(imgSize);
		
		
		generateButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				
				String url = urlBox.getText();
				String path = filePath.getText();
				String name = fileName.getText();
				boolean shortenURL = shortenURLCheckBox.isSelected();
				int errorCorrection = errorCorrectionComboBox.getSelectedIndex();
				String extension = extensions[imageExtensionComboBox.getSelectedIndex()];
				int size = imgSize.getSelectedIndex();
				
				if ((url!="")&&(name!="")&&(path!="")){
					Controller.generateQR(url, name, path, extension, size, errorCorrection, shortenURL);
				}
			}
		});
		
		
	}
}
